package com.godliu.file.demo.controller;

import com.godliu.file.demo.pojo.Person;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class WebRequestDemo {

    @GetMapping(value = "/say", produces = {"application/json;charset=utf-8"})
    public String sayHello(@RequestParam String name, HttpServletRequest request, HttpServletResponse response) {
        //Enumeration<String> headers = request.getHeaderNames();
        //while (headers.hasMoreElements()){
        //    String headerName = (String) headers.nextElement();
        //    System.out.println(headerName + " : " + request.getHeader(headerName) + "<br/>");
        //}
        log.info("sayHello header:" + request.getHeader("acc"));
        Map<String, String> map = new HashMap<>();
        map.put("hello", name);
        return new Gson().toJson(map);
    }

    @PostMapping(value = "/form", produces = {"application/json;charset=utf-8"})
    public String formData(String name, Integer age) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        map.put("age", age + "");
        return new Gson().toJson(map);
    }

    @PostMapping(value = "/person", produces = {"application/json;charset=utf-8"})
    public String person(@RequestBody Person person) {
        return new Gson().toJson(person);
    }
}
